package com.huiminpay.transaction.service.Impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huiminpay.common.cache.Cache;
import com.huiminpay.common.cache.domain.CommonErrorCode;
import com.huiminpay.common.cache.exception.BizException;
import com.huiminpay.transaction.api.dto.PayChannelDTO;
import com.huiminpay.transaction.api.dto.PayChannelParamDTO;
import com.huiminpay.transaction.api.dto.PlatformChannelDTO;
import com.huiminpay.transaction.api.service.IPayService;
import com.huiminpay.transaction.convert.PayChannelParamConvert;
import com.huiminpay.transaction.convert.PlatformChannelConvert;
import com.huiminpay.transaction.entity.AppPlatformChannel;
import com.huiminpay.transaction.entity.PayChannelParam;
import com.huiminpay.transaction.entity.PlatformChannel;
import com.huiminpay.transaction.mapper.AppPlatformChannelMapper;
import com.huiminpay.transaction.mapper.PayChannelMapper;
import com.huiminpay.transaction.mapper.PayChannelParamMapper;
import com.huiminpay.transaction.mapper.PlatformChannelMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @author 王帅坤
 * @date 2021年06月11日14:37
 */
@Service
public class PayServiceImpl implements IPayService {

    @Autowired
    PlatformChannelMapper platformChannelMapper;

    @Autowired
    AppPlatformChannelMapper appPlatformChannelMapper;

    @Autowired
    PayChannelMapper payChannelMapper;

    @Autowired
    PayChannelParamMapper payChannelParamMapper;

    @Autowired
    Cache cache;

    private String keyPre="HUIMINPAY_PAYPARAM";

    @Override
    public List<PlatformChannelDTO> findAllPlatformChannel() {

        List<PlatformChannel> list = platformChannelMapper.selectList(null);
        return PlatformChannelConvert.INSTANCE.listentity2listdto(list);
    }

    Integer getAppServiceCnt(String appId, String platfromChannel) {
        QueryWrapper<AppPlatformChannel> qw = new QueryWrapper<>();
        qw.lambda().eq(AppPlatformChannel::getAppId, appId).eq(AppPlatformChannel::getPlatformChannel, platfromChannel);
        return appPlatformChannelMapper.selectCount(qw);
    }

    Integer appIdIsEmpty(String appID) {
        QueryWrapper<AppPlatformChannel> qw = new QueryWrapper<>();
        qw.lambda().eq(AppPlatformChannel::getAppId, appID);
        return appPlatformChannelMapper.selectCount(qw);
    }

    @Override
    public Integer openAppService(String appId, String platfromChannel) throws BizException {

        Integer count = getAppServiceCnt(appId, platfromChannel);
        if (count > 0) {
            throw new BizException(CommonErrorCode.E_100009);
        }
        Integer integer = appIdIsEmpty(appId);
        if (integer == 0) {
            throw new BizException(CommonErrorCode.E_100010);
        }
        AppPlatformChannel appPlatformChannel = new AppPlatformChannel();
        appPlatformChannel.setAppId(appId);
        appPlatformChannel.setPlatformChannel(platfromChannel);

        return appPlatformChannelMapper.insert(appPlatformChannel);
    }

    @Override
    public Integer queryOpenService(String appId, String platfromChannel) {

        return getAppServiceCnt(appId, platfromChannel);
    }

    @Override
    public List<PayChannelDTO> findPlatfromChannels(String platfromChannelCode) {


        return payChannelMapper.findPlatfromChannels(platfromChannelCode);

    }

    @Override
    public PayChannelParamDTO savePayChannelParam(PayChannelParamDTO payChannelParamDTO) throws BizException{

        //1.获取appPlatformChannelId，根据appId和platformChannelCode

        AppPlatformChannel appPlatformChannel = getAppPlatformChannel(payChannelParamDTO.getAppId(),payChannelParamDTO.getPlatformChannelCode());
        Long appPlatformChannelId = appPlatformChannel.getId();
        if (appPlatformChannelId==null){
            throw new BizException(CommonErrorCode.E_200202);
        }
        //2.保存或者更新

        //2.1.根据获取appPlatformChannelId和payChannel唯一约束

        QueryWrapper<PayChannelParam> qw = new QueryWrapper<>();
        qw.lambda().eq(PayChannelParam::getAppPlatformChannelId,appPlatformChannelId)
                .eq(PayChannelParam::getPayChannel,payChannelParamDTO.getPayChannel());
        PayChannelParam payChannelParam = payChannelParamMapper.selectOne(qw);

        //2.2.判断非空是更新

        PayChannelParam param = PayChannelParamConvert.INSTANCE.dto2entity(payChannelParamDTO);
        param.setId(payChannelParam.getId());
        param.setAppPlatformChannelId(appPlatformChannelId);
        if (payChannelParam==null){
            //数据插入
            payChannelParamMapper.insert(param);
        }else {
            //数据更新
            param.setChannelName(payChannelParamDTO.getChannelName());
            param.setParam(payChannelParamDTO.getParam());
            payChannelParamMapper.updateById(param);
        }
        PayChannelParamDTO dto = PayChannelParamConvert.INSTANCE.entity2dto(param);
        //更新缓存保证数据是最新
        String key = keyPre+":"+payChannelParamDTO.getAppId()+":"+payChannelParamDTO.getPlatformChannelCode();
        cache.del(key);
        cache.set(key,JSON.toJSONString(dto));

        return dto;
    }

    @Override
    public List<PayChannelParamDTO> queryPlatfromChannelPayParams(String appId, String platfromChannel) {

        //升级方法
        String key = keyPre+":"+appId+":"+platfromChannel;//产品名+业务名+参数

        if (cache.exists(key)){
            String s = cache.get(key);
            //解析json
            List<PayChannelParamDTO> list = JSON.parseArray(s, PayChannelParamDTO.class);

            return list;
        }
        //1.获取appPlatformChannelId，根据appId和platformChannelCode

        AppPlatformChannel appPlatformChannel = getAppPlatformChannel(appId,platfromChannel);
        Long appPlatformChannelId = appPlatformChannel.getId();
        if (appPlatformChannelId==null){
            throw new BizException(CommonErrorCode.E_200202);
        }

        QueryWrapper<PayChannelParam> qw = new QueryWrapper<>();
        qw.lambda().eq(PayChannelParam::getAppPlatformChannelId,appPlatformChannelId);
        List<PayChannelParam> list = payChannelParamMapper.selectList(qw);
        List<PayChannelParamDTO> payChannelParamDTOS = PayChannelParamConvert.INSTANCE.listentity2listdto(list);

        //缓存到redis中
        cache.set(key, JSON.toJSONString(payChannelParamDTOS));
        return payChannelParamDTOS;
    }

    @Override
    public PayChannelParamDTO queryPlatformChannelPayParam(String appId, String platformChannel, String payChannel) throws BizException{
        //1.获取appPlatformChannelId，根据appId和platformChannelCode

        AppPlatformChannel appPlatformChannel = getAppPlatformChannel(appId,platformChannel);
        Long appPlatformChannelId = appPlatformChannel.getId();
        if (appPlatformChannelId==null){
            throw new BizException(CommonErrorCode.E_200202);
        }

        QueryWrapper<PayChannelParam> qw = new QueryWrapper<>();
        qw.lambda().eq(PayChannelParam::getAppPlatformChannelId,appPlatformChannelId)
        .eq(PayChannelParam::getPayChannel,payChannel);
        PayChannelParam payChannelParam = payChannelParamMapper.selectOne(qw);
        return PayChannelParamConvert.INSTANCE.entity2dto(payChannelParam);
    }

    /*
     *获取应用绑定服务
     */

    private AppPlatformChannel getAppPlatformChannel(String appId, String platfromChannel) {
        QueryWrapper<AppPlatformChannel> qw = new QueryWrapper<>();
        qw.lambda().eq(AppPlatformChannel::getAppId, appId)
                .eq(AppPlatformChannel::getPlatformChannel, platfromChannel);
        return appPlatformChannelMapper.selectOne(qw);
    }
}
